package com.lpy.book.JFrame;

import com.lpy.book.dao.BookDao;
import com.lpy.book.model.Book;
import com.lpy.book.model.BookType;
import com.lpy.book.model.DbUtil;
import com.lpy.book.utils.ToolUtil;
import com.mysql.jdbc.Connection;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import static sun.plugin.ClassLoaderInfo.reset;

public class AdminBookAdd {

    JFrame jf;
    JTextField textField;
    JTextField textField_1;
    JTextField textField_2;
    JTextField textField_3;
    JTextField textField_4;
    JTextField textField_6;
    ToolUtil toolUtil = new ToolUtil();


     public AdminBookAdd() {
         jf = new JFrame();
         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         jf.setTitle("111");
         jf.setSize(600, 800);
         jf.setLocation(400, 200);
         jf.setVisible(true);
         JPanel panel = new JPanel();
         panel.setBorder(new TitledBorder(null, "\u4E66\u7C4D\u6DFB\u52A0", TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED));
         panel.setBounds(23, 21, 540, 275);
         jf.getContentPane().add(panel);
         panel.setLayout(null);
         JLabel lblNewLabel = new JLabel("书名：");
         lblNewLabel.setFont(new Font("幼圆", Font.BOLD, 14));
         lblNewLabel.setBounds(58, 31, 45, 27);
         panel.add(lblNewLabel);
         textField = new JTextField();
         textField.setBounds(101, 31, 129, 27);
         panel.add(textField);
         textField.setColumns(10);
         JLabel label = new JLabel("作者：");
         label.setFont(new Font("幼圆", Font.BOLD, 14));
         label.setBounds(294, 31, 45, 27);
         panel.add(label);
         textField_1 = new JTextField();
         textField_1.setColumns(10);
         textField_1.setBounds(338, 31, 128, 27);
         panel.add(textField_1);
         JLabel label_1 = new JLabel("出版社：");
         label_1.setFont(new Font("幼圆", Font.BOLD, 14));
         label_1.setBounds(43, 79, 60, 27);
         panel.add(label_1);

         textField_2 = new JTextField();
         textField_2.setColumns(10);
         textField_2.setBounds(101, 79, 129, 27);
         panel.add(textField_2);

         JLabel label_2 = new JLabel("库存：");
         label_2.setFont(new Font("幼圆", Font.BOLD, 14));
         label_2.setBounds(58, 125, 45, 27);
         panel.add(label_2);

         textField_3 = new JTextField();
         textField_3.setColumns(10);
         textField_3.setBounds(101, 125, 129, 27);
         panel.add(textField_3);

         JLabel label_3 = new JLabel("价格：");
         label_3.setFont(new Font("幼圆", Font.BOLD, 14));
         label_3.setBounds(294, 79, 45, 27);
         panel.add(label_3);

         textField_4 = new JTextField();
         textField_4.setColumns(10);
         textField_4.setBounds(337, 79, 129, 27);
         panel.add(textField_4);

         JLabel label_4 = new JLabel("类别：");
         label_4.setFont(new Font("幼圆", Font.BOLD, 14));
         label_4.setBounds(294, 125, 45, 27);
         panel.add(label_4);

         JLabel label_5 = new JLabel("描述：");
         label_5.setFont(new Font("幼圆", Font.BOLD, 14));
         label_5.setBounds(58, 170, 45, 27);
         panel.add(label_5);

         textField_6 = new JTextField();
         textField_6.setColumns(10);
         textField_6.setBounds(101, 173, 365, 27);
         panel.add(textField_6);

         JButton btnNewButton = new JButton("添加");
         btnNewButton.setBounds(220, 220, 100, 30);
         btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String bookName = textField.getText();
                String author = textField_1.getText();
                String publish = textField_2.getText();
                String priceStr = textField_4.getText();
                String numberStr = textField_3.getText();
                String remark = textField_6.getText();
                if (toolUtil.isEmpty(bookName) || toolUtil.isEmpty(author) || toolUtil.isEmpty(publish) || toolUtil.isEmpty(priceStr) || toolUtil.isEmpty(numberStr) || toolUtil.isEmpty(remark)) {
                    JOptionPane.showMessageDialog(null, "请输入相关内容");
                    return;
                }
                ComboBoxModel<Object> comboBox = null;
                BookType selectedItem = (BookType)comboBox.getSelectedItem();
                Integer typeId = selectedItem.getTypeId();
                int number;
                double price;
                try {
                    number = Integer.parseInt(numberStr);
                    price = new BigDecimal(priceStr).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "参数错误");
                    return;
                }
                Book book = new Book();
                book.setBookName(bookName);
                book.setAuthor(author);
                book.setBookTypeId(typeId);
                book.setNumber(number);
                book.setPrice(price);
                book.setPublish(publish);
                book.setRemark(remark);
                book.setStatus(1);
                Connection con = null;
                try {
                    con = DbUtil.getConnection();
                    int i = BookDao.add(con, book);
                    if (i == 1) {
                        JOptionPane.showMessageDialog(null, "添加成功");
                        reset();
                    } else {
                        JOptionPane.showMessageDialog(null, "添加失败");
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                    JOptionPane.showMessageDialog(null, "添加异常");
                }
            }
         });
         panel.add(btnNewButton);
     }

    public static void main(String[] args) {
        AdminBookAdd add = new AdminBookAdd();
    }

}
